/*
 * SharePathOptionWindow.java
 *
 * Created on __DATE__, __TIME__
 */

package Windows.Share;

import java.io.File;

import javax.swing.JFrame;
import javax.swing.WindowConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import DBUtil.CNN;
import Tools.IMG;
import Tools.Units;
import Windows.MainWindows;
import Windows.PicPanel;
import Windows.ShowWindows.ShowWindow;
import XML.XMLNode;

import com.sunking.swing.JDirChooser;

/**
 * 
 * @author __USER__
 */
public class SharePathOptionWindow extends JFrame implements DocumentListener {

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				new SharePathOptionWindow().setVisible(true);
			}
		});
	}

	File f;

	// GEN-END:initComponents

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;

	private javax.swing.JButton jButton2;

	private javax.swing.JLabel jLabel1;

	private javax.swing.JLabel jLabel2;

	private javax.swing.JPanel jPanel1;
	public javax.swing.JTextField jTextField1;
	/** Creates new form SharePathOptionWindow */
	public SharePathOptionWindow() {
		setContentPane(new PicPanel(IMG.share));
		jPanel1 = new PicPanel(IMG.banner);
		initComponents();
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		jTextField1.getDocument().addDocumentListener(this);
		this.addWindowListener(ShowWindow.getWindowListener());
		Tools.ToCenter.ToCenter(this);
	}
	@Override
	public void changedUpdate(DocumentEvent arg0) {
		// TODO Auto-generated method stub

	}
	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();

		jLabel2 = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("\u5171\u4eab\u8bbe\u7f6e");

		jLabel1.setText("\u5171\u4eab\u76ee\u5f55\uff1a");

		jButton1.setText("jButton1");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jButton2.setText("\u8bbe\u7f6e");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		jPanel1.setBackground(new java.awt.Color(153, 255, 255));

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 398, Short.MAX_VALUE));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 83, Short.MAX_VALUE));

		jLabel2.setText("\u5171\u4eab\u5bb9\u91cf\uff1a");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addComponent(jLabel1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE).addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(18, 18, 18).addComponent(jButton2).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(47, 47, 47).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1).addComponent(jButton1).addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jButton2)).addGap(18, 18, 18).addComponent(jLabel2).addContainerGap(23, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	@Override
	public void insertUpdate(DocumentEvent arg0) {
		// TODO Auto-generated method stub
		f = new File(jTextField1.getText());
		jLabel2.setText("  剩余空间:" + Units.byteCondense(f.getFreeSpace()) + "　　　　磁盘空间:" + Units.byteCondense(f.getTotalSpace()));
	}

	// End of variables declaration//GEN-END:variables

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		f = JDirChooser.showDialog(this, "请选择共享位置", true, null, "请选择");
		if (f != null) {
			jTextField1.setText(f.getAbsolutePath());

		}
	}

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		if (f != null && f.isDirectory()) {
			XMLNode xn = (XMLNode) CNN.getConfig().GetPathValue("User.Share");
			if (xn != null) {
				xn.put("dir", f.getPath());
				CNN.getConfig().createCode(new File("Config.xml"));
			}

			MainWindows.getInstance().client.sp.setF(f);
			this.setVisible(false);
//			new Thread() {
//				public void run() {
//					java.awt.EventQueue.invokeLater(new Runnable() {
//						public void run() {
//							ShowWindow.ConcealWindow(
//									SharePathOptionWindow.this, 2);
//						}
//
//					});
//				}
//			}.start();
		}

	}

	@Override
	public void removeUpdate(DocumentEvent arg0) {
		// TODO Auto-generated method stub
		f = new File(jTextField1.getText());
		jLabel2.setText("  剩余空间:" + Units.byteCondense(f.getUsableSpace()) + "　　　　磁盘空间:" + Units.byteCondense(f.getTotalSpace()));
	}

}